<template>
    <div class="app-container">
        <section>
            <!-- 关于玉丹颜 -->
            <div class="content">
                <el-form ref="form" :model="form" class="form" :rules="rules">
                    <el-form-item label="首页封面图（PC）" prop="value_pc" style="width:224px">
                        <div class="img" v-if="form.value_pc">
                            <el-image :src="form.value_pc"  :lazy="true"  fit="fill" style="width: 100%; height: 100%" :preview-src-list="form.srcList">
                                <div slot="error" class="image-slot"   style="width: 100%;height: 100%;background-color: #e6e6e6;"></div>
                            </el-image>
                            <i class="el-icon-delete" @click="form.value_pc = ''"></i>
                        </div>
                        <el-upload :action="$Reg.uploadUrl" list-type="picture-card"  :http-request="okUpload" :data="flag" v-else :show-file-list="false">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="首页封面图（移动）" prop="value" style="width:224px">
                        <div class="img" v-if="form.value">
                            <el-image :src="form.value"  :lazy="true"  fit="fill" style="width: 100%; height: 100%" :preview-src-list="form.srcList1">
                                <div slot="error" class="image-slot"   style="width: 100%;height: 100%;background-color: #e6e6e6;"></div>
                            </el-image>
                            <i class="el-icon-delete" @click="form.value = ''"></i>
                        </div>
                        <el-upload :action="$Reg.uploadUrl" list-type="picture-card"  :http-request="okUpload" v-else :show-file-list="false">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <div class="sure">
                    <el-button type="primary" style="width:143px" @click="save">确定</el-button>
                </div>
            </div>

        </section>
    </div>
</template>

<script>
export default {
    data() {
        return {
            flag: {
                a: 0
            },
            form: {
                value: '',
                value_pc: ''
            },
            rules: {
                value: [
                    { required: true, message: '请上传首页封面图（移动）', trigger: 'blur' },
                ],
                value_pc: [
                    { required: true, message: '请上传首页封面图（PC）', trigger: 'blur' },
                ],
            }
        }
    },
    created() {
    },
    mounted() {
        this.getHomeCover()
    },
    methods: {
        async getHomeCover() {
            let res = await this.$Http.getHomeCover()
            if (res.status == 1) {
                this.form.value = res.data.value
                this.form.value_pc = res.data.value_pc
                this.form.srcList = [this.form.value_pc]
                this.form.srcList1 = [this.form.value]
            } else {
                this.$message.error(res.msg)
            }
        },
        save() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    let res = await this.$Http.setHomeCover(this.form)
                    if (res.status == 1) {
                        this.$message.success('保存成功')
                    }
                } else {
                    return false;
                }
            });
        },
        //上传头像文件等

        okUpload(file) {
            let formData = new window.FormData();
            formData.append("files", file.file);
            this.$Http.upload(formData).then((res) => {
                if (res.status == 1) {
                    this.$message({
                        message: "上传成功",
                        type: "success",
                    });
                    if (file.data) {
                        this.form.value_pc = res.data
                    } else {
                        this.form.value = res.data
                    }
                } else {
                    this.$message(res.msg);
                }
            });
        },
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    .head-tabs {
        height: 40px;
        width: 100%;
        padding-left: 30px;

        ::v-deep {
            .el-tabs__nav-wrap::after {
                background-color: transparent;
            }
            .el-tabs__active-bar {
                background-color: rgb(64, 158, 255);
            }
            .el-tabs__item {
                height: 40px;
                line-height: 30px;
                font-size: 16px;
                font-weight: 400;
                text-align: left;
                letter-spacing: 1px;
                color: #364164;
            }
            .is-active {
                font-weight: 600 !important;
                color: rgb(64, 158, 255);
            }
        }
    }
    .img {
        width: 146px;
        height: 146px;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        transition: all 3s;
        &:hover {
            i {
                display: flex;
                align-items: center;
                justify-content: center;
                background: rgba($color: #000000, $alpha: 0.3);
                color: rgba($color: #fff, $alpha: 1);
            }
        }
        i {
            position: absolute;
            color: #fff;
            font-size: 26px;
            display: none;
        }
    }
    ::v-deep .el-form-item__label {
        float: initial;
    }
    ::v-deep .el-input.is-disabled .el-input__inner {
        color: #606266;
        background-color: transparent;
    }
    section {
        background: #f7fafc;
        padding: 30px;
        .content {
            width: 100%;
            height: 100%;
            background: #fff;
            border-top-left-radius: 16px;
            border-top-right-radius: 16px;
            padding: 20px;
            .form {
                display: flex;
                flex-wrap: wrap;
            }
        }
    }
}
</style>